/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.developer.spoti.vspot.slice;

import com.developer.spoti.vspot.ResourceTable;

import com.developer.spoti.vspot.utils.CustomText;
import com.developer.spoti.vspoti.VspotView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.AbilityInfo;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;

/**
 * MainAbilitySlice
 *
 * @author AnBetter
 * @since 2021-06-22
 */
public class MainAbilitySlice extends AbilitySlice {
    int a = 0;
    private VspotView mVspotView;
    private VspotView.Builder builder;
    private Component root;
    private Component view1;
    private Component view2;
    private Component view3;
    private TextField view4;
    private Component view5;
    private CustomText customText;
    private Component scrollView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        setBarColor();
        root = findComponentById(ResourceTable.Id_root);
        view1 = findComponentById(ResourceTable.Id_view1);
        view2 = findComponentById(ResourceTable.Id_view2);
        view3 = findComponentById(ResourceTable.Id_view3);
        view4 = (TextField)findComponentById(ResourceTable.Id_view4);
        view5 = findComponentById(ResourceTable.Id_view5);
        scrollView = findComponentById(ResourceTable.Id_scrollview);
        show();
        view3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
            }
        });
    }

    /**
     * show
     */
    private void show() {
        builder = new VspotView.Builder(this)
                .setTitle("Guide Title Text")
                .setContentText(getString(ResourceTable.String_content_Text))
                .setGravity(VspotView.Gravity.center)
                .setDismissType(VspotView.DismissType.outside)
                .setTargetView(view1)
                .setVspotListener(view -> {
                    switch (view.getId()) {
                        case ResourceTable.Id_view1:
                            builder.setTargetView(view2).build();
                            break;
                        case ResourceTable.Id_view2:
                            builder.setTargetView(view3).build();
                            break;
                        case ResourceTable.Id_view3:
                            builder.setTargetView(view4).build();
                            break;
                        case ResourceTable.Id_view4:
                            builder.setTargetView(view5).build();
                            break;
                        case ResourceTable.Id_view5:
                            return;
                        default:
                            return;
                    }
                    mVspotView = builder.build();
                    mVspotView.show();
                });

        mVspotView = builder.build();
        mVspotView.show();
    }

    /**
     * 设置TitleBar颜色
     */
    private void setBarColor() {
        ohos.global.resource.ResourceManager resManager = this.getResourceManager();
        int mcolor = 0;
        try {
            mcolor = resManager.getElement(ResourceTable.Color_myblue).getColor();
        } catch (IOException e) {
            e.getMessage();
        } catch (NotExistException e) {
            e.getMessage();
        } catch (WrongTypeException e) {
            e.getMessage();
        }
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(mcolor); // 设置状态栏颜色
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
        super.onOrientationChanged(displayOrientation);
        if(AbilityInfo.DisplayOrientation.LANDSCAPE.name().equals(displayOrientation.name())){
            scrollView.setHeight(AttrHelper.vp2px(320,this));
        }else {
            scrollView.setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        }
    }


}
